package com.czy.controller;

import com.czy.pojo.PageResult;
import com.czy.pojo.Result;
import com.czy.pojo.Student;
import com.czy.pojo.StudentPage;
import com.czy.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/students")
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 条件分页查询学员信息
     */
    @GetMapping
    public Result list(StudentPage page) {
        log.info("条件分页查询学员信息：{}", page);
        PageResult<Student> pageList = studentService.page(page);
        return Result.success(pageList);
    }

    /**
     * 添加学员信息
     */
    @PostMapping
    public Result add(@RequestBody Student student) {
        log.info("添加学员信息：{}", student);
        studentService.insert(student);
        return Result.success();
    }

    /**
     * 根据ID查询学员信息
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("根据ID查询学员信息：{}", id);
        Student student = studentService.findById(id);
        return Result.success(student);
    }

    /**
     * 修改学员信息
     */
    @PutMapping
    public Result update(@RequestBody Student student) {
        log.info("修改学员信息：{}", student);
        studentService.update(student);
        return Result.success();
    }

    /**
     * 批量删除学员信息
     */
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable ArrayList<Integer> ids) {
        log.info("批量删除学员信息：{}", ids);
        studentService.delete(ids);
        return Result.success();
    }

    /**
     * 添加违纪处理
     */
    @PutMapping("/violation/{id}/{score}")
    public Result addViolation(@PathVariable Integer id,
                               @PathVariable Integer score){
        log.info("添加违纪处理：{}，{}" ,id,score);
        studentService.updateViolation(id,score);
        return Result.success();

    }
}
